/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Constant;
import com.hboxs.asl.dao.PaymentMethodDao;
import com.hboxs.asl.dao.YiYuanTradeDao;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.pingpp.Constants;
import com.hboxs.asl.service.*;
import com.pingplusplus.model.Charge;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * Service - 支付查看答案
 */
@Service("yiYuanTradeServiceImpl")
public class YiYuanTradeServiceImpl extends BaseServiceImpl<YiYuanTrade, Long> implements YiYuanTradeService {

    @Resource(name = "yiYuanTradeDaoImpl")
    private YiYuanTradeDao yiYuanTradeDao;
    @Resource(name = "snServiceImpl")
    private SnService snService;
    @Resource(name= "paymentMethodDaoImpl")
    private PaymentMethodDao paymentMethodDao;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "financialRecordServiceImpl")
    private FinancialRecordService financialRecordService;

    @Resource(name = "yiYuanTradeDaoImpl")
    public void setBaseDao(YiYuanTradeDao yiYuanTradeDao) {
        super.setBaseDao(yiYuanTradeDao);
    }


    @Override
    public void dealYiYuanTrade(Charge charge, YiYuanTrade yiYuanTrade) {

        PaymentMethod paymentMethod = findPaymentMethod(charge.getChannel());

        yiYuanTrade.setPaymentMethod(paymentMethod);
        yiYuanTrade.setPaymentMethodName(paymentMethod.getName());

        yiYuanTrade.setPaymentStatus(YiYuanTrade.PaymentStatus.paid);
        Question question = questionService.find(yiYuanTrade.getQuestionId());
        if (question != null) {
            Member member = question.getMember();
            if (member != null) {
                BigDecimal profit = yiYuanTrade.getPrice().divide(BigDecimal.valueOf(2));
                //用户提成
                member.setBalance(member.getBalance().add(profit));
                //我的收入
                member.setEarnings(member.getEarnings().add(profit));
                memberService.update(member);
            }
        }
        //1元查看答案支出记录
        financialRecordService.expenditureRecordYiYuan(yiYuanTrade, null);
        //1元查看答案收入记录
        financialRecordService.incomeRecordYiYuan(yiYuanTrade, null);
        this.update(yiYuanTrade);
    }

    @Override
    public void dealYiYuanTrade(YiYuanTrade yiYuanTrade) {

        PaymentMethod paymentMethod = findPaymentMethod(Constants.POINT_AMOUNT_PAY);

        yiYuanTrade.setPaymentMethod(paymentMethod);
        yiYuanTrade.setPaymentMethodName(paymentMethod.getName());

        yiYuanTrade.setPaymentStatus(YiYuanTrade.PaymentStatus.paid);
        //1元查看答案支出记录
        financialRecordService.expenditureRecordYiYuan(yiYuanTrade, Constant.POINT_PAY);
        //1元查看答案收入记录
        financialRecordService.incomeRecordYiYuan(yiYuanTrade, Constant.POINT_PAY);
        this.update(yiYuanTrade);
    }

    @Override
    public YiYuanTrade findBySn(String sn) {
        return yiYuanTradeDao.findBySn(sn);
    }

    @Override
    public String generateYiYuan(Question question, Member member) {
        YiYuanTrade yiYuanTrade = new YiYuanTrade();
        yiYuanTrade.setSn(snService.generate(Sn.Type.defray));
        yiYuanTrade.setBuyer(member);
        yiYuanTrade.setQuestionId(question.getId());
        yiYuanTrade.setPaymentStatus(YiYuanTrade.PaymentStatus.unpaid);
        yiYuanTrade.setPrice(BigDecimal.valueOf(1));
        this.save(yiYuanTrade);
        return yiYuanTrade.getSn();
    }

    @Override
    public long isPay(Member member, Long questionId) {
        return yiYuanTradeDao.isPay(member, questionId);
    }

    /**
     * 查找对应的支付方式
     */
    private PaymentMethod findPaymentMethod(String channel) {
        List<PaymentMethod> paymentMethods = paymentMethodDao.findList(null, null, null, null);
        HashMap<String, Integer> modeOfPayment = new HashMap<>();
        modeOfPayment.put(Constants.CHANNEL_WX, 0);
        modeOfPayment.put(Constants.CHANNEL_ALIPAY, 1);
        modeOfPayment.put(Constants.CHANNEL_UPACP, 2);
        modeOfPayment.put(Constants.CHANNEL_AMOUNT_PAY, 3);
        modeOfPayment.put(Constants.POINT_AMOUNT_PAY, 5);
        return paymentMethods.get(modeOfPayment.get(channel));
    }
}
